<?php

class xiecx_loader
{

    protected static $models;
    protected static $modules;
    protected static $libraries;

    public function __construct()
    {

        self::$models = new stdClass;
        self::$modules = new stdClass;
        self::$libraries = new stdClass;
    }

    public static function view($file, $data, $module_name)
    {
        $file = self::split_filename($file);
    }

    public static function &model($file, $name='', $module_name='')
    {
        $file = self::split_filename($file);
        if (!isset($name{1}))
        {
            $name = $file['name'];
        }

        if (isset(self::$models->{$name}))
        {
            return self::$models->{$name};
        }
        else
        {
            return self::$models->{$name} = & self::load_file('model', $file, $module_name);
        }
    }

    public static function library($file, $name='')
    {

        if (isset(self::$libraries->$name))
        {
            return self::$libraries->$name;
        }
        else
        {
            if (!class_exists($file))
            {
                load_class($file, 'library');
            }

            return self::$libraries->$name = new $file();
        }
    }

    public static function &module($name, $initialize=true)
    {
        
    }

    protected static function split_filename($filename)
    {
        $parts = explode('/', trim($filename));
        $name = end(str_replace(EXT, '', $parts));

        return array(
            'path' => str_replace($name, '', $filename)
            , 'name' => $name
            , 'fullname' => $filename
        );
    }

    protected static function load_file($type, $file, $module_name ='')
    {
        $dir = APP_PATH;
        if (isset($module_name{5}))
        {
            $dir .= $module_name . '/';
        }

        switch ($type)
        {
            case 'model':
                break;

            case 'library':
                break;

            case 'module':
                break;
        }
    }

}